package com.acwing.partition3;

import java.io.*;

/**
 * @author `RKC`
 * @date 2022/2/6 15:25
 */
public class AC257关押罪犯 {

    private static final int N = 20010, M = 200010;
    private static int[] h = new int[N], e = new int[M], ne = new int[M], w = new int[M];
    private static int[] color = new int[N];

    private static int n = 0, m = 0, idx = 1;

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        String[] ss = reader.readLine().split(" ");
        n = Integer.parseInt(ss[0]);
        m = Integer.parseInt(ss[1]);
        for (int i = 0; i < m; i++) {
            ss = reader.readLine().split(" ");
            int a = Integer.parseInt(ss[0]), b = Integer.parseInt(ss[1]), c = Integer.parseInt(ss[2]);
            add(a, b, c);
            add(b, a, c);
        }
        int left = 0, right = (int) 1e9;
        while (left < right) {
            int mid = (left + right) >> 1;
            //最大仇恨值能正常的构成二分图，就尝试继续缩小仇恨值
            if (check(mid)) right = mid;
            else left = mid + 1;
        }
        writer.write(left + "\n");
        writer.flush();
    }

    private static boolean check(int limit) {
        //当前仇恨值的情况下判定二分图，如果边权超过了limit，说明两个罪犯不能在一个监狱，需要进行染相反色
        for (int i = 0; i < N; i++) color[i] = 0;
        for (int i = 1; i <= n; i++) {
            if (color[i] != 0) continue;
            if (!dfs(i, 1, limit)) return false;
        }
        return true;
    }

    private static boolean dfs(int u, int col, int limit) {
        color[u] = col;
        for (int i = h[u]; i != 0; i = ne[i]) {
            int v = e[i];
            if (w[i] <= limit) continue;
            if (color[v] == col) return false;
            if (color[v] == 0 && !dfs(v, 3 - col, limit)) return false;
        }
        return true;
    }

    private static void add(int a, int b, int c) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx;
        w[idx++] = c;
    }
}
